#include "utils.h"

uint8_t buf[10];

void intToString(int value, uint8_t* pBuf, uint32_t len, uint32_t base)
{
    static const char* pAscii = "0123456789abcdefghijklmnopqrstuvwxyz";
    int pos = 0;
    int tmpValue = value;

    // the buffer must not be null and at least have a length of 2 to handle one
    // digit and null-terminator
    if (pBuf == 0 || len < 2)
    {
        return;
    }

    // a valid base cannot be less than 2 or larger than 36
    // a base value of 2 means binary representation. A value of 1 would mean only zeros
    // a base larger than 36 can only be used if a larger alphabet were used.
    if (base < 2 || base > 36)
    {
        return;
    }

    // negative value
    if (value < 0)
    {
        tmpValue = -tmpValue;
        value    = -value;
        pBuf[pos++] = '-';
    }

    // calculate the required length of the buffer
    do {
        pos++;
        tmpValue /= base;
    } while(tmpValue > 0);


    if (pos > len)
    {
        // the len parameter is invalid.
        return;
    }

    pBuf[pos] = '\0';

    do {
        pBuf[--pos] = pAscii[value % base];
        value /= base;
    } while(value > 0);

    return;

}
//int roundf( float f )
//{
//  int aux = (int)f;
//  f = f - aux;
//  return f > 0.5f? aux + 1 : aux ;
//}

//float atof(const char* s){
//  float rez = 0, fact = 1;
//  if (*s == '-'){
//    s++;
//    fact = -1;
//  };
//  for (int point_seen = 0; *s; s++){
//    if (*s == '.'){
//      point_seen = 1; 
//      continue;
//    };
//    int d = *s - '0';
//    if (d >= 0 && d <= 9){
//      if (point_seen) fact /= 10.0f;
//      rez = rez * 10.0f + (float)d;
//    };
//  };
//  return rez * fact;
//}
//char *strconcat(char *dst, char *src, uint8_t size)
//{
//    char *fdst = (char*)malloc(sizeof(char)*size);
//    char *ini = fdst;
//    if (*dst == '\0' || *src == '\0')
//        return fdst;
//    
//    while (*dst != '\0')
//    {
//        *fdst++ = *dst++;
//    }
//    while (*src != '\0')
//    {
//        *fdst++ = *src++;
//    }
//    
//    *fdst = '\0';
//    return ini;
//}
void CoordenadaToStr(float coord, int* graus, int* minutos, int*segundos)
{
    *graus = (int)coord;
    coord = coord - *graus;
    coord *= 60;
    *minutos = (int)coord;
    coord = coord - *minutos;
    coord *= 60;
    *segundos = (int)coord;
}